package com.hd.view.action;


import java.text.SimpleDateFormat;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;

import com.hd.common.DataPage;
import com.hd.persistence.model.Person;
import com.hd.utils.RequestUtils;


/**
 * 内部人员管理Action
 * 
 */

public class MemberAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/*登录名*/
	private String loginName;
	/*登录密码*/
	private String password;
	
	private Person person;
	/*出生日期*/
	private String birthday;
	/*入职时间*/
	private String beginTime;
	/*职工姓名*/
	private String name;
	/*查询的员工*/
	private DataPage<Person> persons;
	/**
	 * 系统登录
	 * 
	 * 将用户名和密码放入cookie用于跳转时验证
	 * 
	 * @return
	 * @throws Exception
	 */
	public String login() throws Exception{		
		/*验证登录*/
		if(StringUtils.isEmpty(loginName) && StringUtils.isEmpty(password)){
			return INPUT;
		}
		
		Person person = memberManager.findPersonBy(loginName, password);
			
		if(person == null){
			message = "登录名或密码不正确";
			return INPUT;
		}
		
		
		/*登录成功后在cookie中设置loginName,password,userInfo*/
		//将loginName放入Cookie
		Cookie cookieLoginName = new Cookie("loginName",loginName);
		cookieLoginName.setPath("/");
		getResponse().addCookie(cookieLoginName);
		
		//将password放入Cookie
		Cookie cookiePassword = new Cookie("password",password);
		cookiePassword.setPath("/");
		getResponse().addCookie(cookiePassword);
		
		//将userInfo放入Cookie,在过滤器中验证时如果 loginName + "_" + password = userInfo,验证通过
		String userInfo = loginName + "_" + password;
		Cookie cookieUserInfo = new Cookie("userInfo",userInfo);
		cookieUserInfo.setPath("/");
		getResponse().addCookie(cookieUserInfo);
		
		
		
		return SUCCESS;
	}
	
	/**
	 * 系统首页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String home() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 退出系统
	 * 
	 * @return
	 * @throws Exception
	 */
	public String exitSystem() throws Exception{
		RequestUtils.removeCookie(getResponse(), "loginName");
		RequestUtils.removeCookie(getResponse(), "password");
		RequestUtils.removeCookie(getResponse(), "userInfo");
		
		return SUCCESS;
	}
	
	/**
	 * 修改密码
	 * 
	 * @return 
	 * @throws Exception
	 */
	public String editPsw() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 增加新员工
	 * 
	 * 注:新员工有角色之分
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addNewEmployee() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 保存新员工
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveNewEmployee() throws Exception{
		if (!StringUtils.isEmpty(birthday)) {
			SimpleDateFormat birthdayTemp = new SimpleDateFormat("yyyy-MM-dd");
			person.setBirthday(birthdayTemp.parse(birthday));
		}

		if (!StringUtils.isEmpty(beginTime)) {
			SimpleDateFormat beginTimeTemp = new SimpleDateFormat("yyyy-MM-dd");
			person.setBeginTime(beginTimeTemp.parse(beginTime));
		}
		
		person.setIsAdmin(false);
		person.setStatus(0);
		memberManager.savePerson(person);
		
		statusTemp = true;
		return SUCCESS;
	}
	
	/**
	 * 修改员工信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editEmployee() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 删除员工
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delEmployee() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 查询员工信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listEmployee() throws Exception{
		persons = memberManager.pageQueryPerson(name, null, getCurrentPage(), pageSize);
		setTotalPage(persons.getTotalPageCount());
		return SUCCESS;
	}
	
	/**
	 * 查看员工信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String viewEmployee() throws Exception{
		return SUCCESS;
	}
	
	
/*========get,set方法=================================================*/
	
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public DataPage<Person> getPersons() {
		return persons;
	}

	public void setPersons(DataPage<Person> persons) {
		this.persons = persons;
	}

	
}
